<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<style>
  body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    margin: 0;
    padding: 0;
    background-color: #f4f4f9;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
  }

  .cart-container {
    width: 90%;
    max-width: 800px;
    background-color: #fff;
    border-radius: 12px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    overflow: hidden;
  }

  .cart-header {
    background-color: #f0f0f0;
    padding: 15px 20px;
    text-align: center;
    font-size: 20px;
    font-weight: bold;
    color: #333;
  }

  .cart-item {
    display: flex;
    align-items: center;
    padding: 15px 20px;
    border-bottom: 1px solid #e0e0e0;
  }

  .cart-item:last-child {
    border-bottom: none;
  }

  .cart-item img {
    width: 70px;
    height: 70px;
    margin-right: 15px;
    border-radius: 8px;
  }

  .cart-item-info {
    flex: 1;
  }

  .cart-item-title {
    font-size: 16px;
    margin-bottom: 5px;
    color: #333;
  }

  .cart-item-specs {
    color: #888;
    margin-bottom: 5px;
  }

  .cart-item-price {
    color: #e4393c;
    font-size: 18px;
    margin-right: 20px;
  }

  .cart-item-quantity {
    display: flex;
    align-items: center;
  }

  .cart-item-quantity button {
    width: 30px;
    height: 30px;
    border: 1px solid #ddd;
    background-color: #fff;
    cursor: pointer;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: background-color 0.3s;
  }

  .cart-item-quantity button:hover {
    background-color: #f0f0f0;
  }

  .cart-item-quantity input {
    width: 40px;
    text-align: center;
    border: none;
    margin: 0 10px;
    border-radius: 4px;
    padding: 5px;
  }

  .cart-summary {
    display: flex;
    justify-content: space-between;
    padding: 15px 20px;
    background-color: #f0f0f0;
  }

  .cart-summary div {
    text-align: center;
  }

  .cart-summary-price {
    color: #e4393c;
    font-size: 20px;
    font-weight: bold;
  }

  .cart-summary button {
    background-color: #e4393c;
    color: #fff;
    border: none;
    padding: 10px 20px;
    cursor: pointer;
    border-radius: 5px;
    transition: background-color 0.3s;
  }

  .cart-summary button:hover {
    background-color: #d02c2c;
  }
</style>


<div class="cart-container">
  <div class="cart-header">购物车</div>
  <div class="cart-item" v-for="(item,index) in arr" :key="item.id">
    <input type="checkbox" @click="vel(item.id)" v-model="item.is">
    <img :src="item.img">
    <div class="cart-item-info">
      <div class="cart-item-title">{{item.name}}</div>
      <div class="cart-item-specs">{{item.title}}</div>
      <div class="cart-item-price">{{item.price | fn()}}</div>
    </div>
    <div class="cart-item-quantity">
      <button @click="jian(item.id)">-</button>
      <input type="text" :value="item.num" readonly>
      <button @click="jia(item.id)">+</button>
    </div>
    <div class="cart-item-actions">
      <button @click="del(index)">删除</button>
      <!-- <button>移入关注</button> -->
    </div>
  </div>
  <!-- <div class="cart-item">
    <input type="checkbox">
    <img src="">
    <div class="cart-item-info">
      <div class="cart-item-title"></div>
      <div class="cart-item-specs"></div>
      <div class="cart-item-price">¥</div>
    </div>
    <div class="cart-item-quantity">
      <button>-</button>
      <input type="text" value="1" readonly>
      <button>+</button>
    </div>
    <div class="cart-item-actions">
      <button>删除</button>
      <button>移入关注</button>
    </div>
  </div> -->
  <div class="cart-summary">
    <div>
      <!-- 已选择 <span>2</span> 件商品 -->
      <!-- 总价：<span class="cart-summary-price">{{jiage}}</span> -->
      总价：<span class="cart-summary-price">{{strs | zon()}}</span>
      <!-- 促销：-¥60.00 -->
    </div>
    <!-- <button>去结算</button> -->
  </div>
</div>

<script>
  new Vue({
    el: '.cart-container',
    data: {
      arr: [{
        id: 1,
        img: 'https://img.shetu66.com/2023/06/28/1687920981963810.png',
        name: '小米13 徕卡光学镜头 第二代骁龙8处理器 超窄边屏幕 120Hz高刷 67W快充',
        title: '黑色 12GB+256GB',
        price: 4599.00,
        num: 1,
        is: false
      }, {
        id: 2,
        img: 'https://img.shetu66.com/2023/06/28/1687920981963810.png',
        name: 'Redmi K50 天玑8100 2K柔性直 O屏IS光学防抖 67W快充 5500mAh大电量',
        title: '幽芒 12GB+256GB',
        price: 2399.00,
        num: 1,
        is: false
      }],
      // zong: 0,
      jiage: 0
    },
    //点击事件
    methods: {
      del(index) {
        this.arr.splice(index, 1)
      },
      vel(id) {
        var a = this.arr.find(item => item.id == id).is = !this.arr.find(item => item.id == id).is
      },
      jia(id) {
        var a = this.arr.find(item => item.id == id)
        a.num++
      },
      jian(id) {
        var a = this.arr.find(item => item.id == id)
        if (a.num != 1) {
          a.num--
        }
      }
    },
    //计算
    computed: {
      strs() {
        var jiage = 0
        this.arr.forEach(item => {
          if (item.is) {
            jiage += item.price * item.num
          }
        });
        return jiage
      }
    },
    //过滤器  
    filters: {
      fn(price) {
        return '￥' + price.toFixed(2)
      },
      zon(jiage) {
        return '￥' + jiage.toFixed(2)
      },
    }
  })
</script>